<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;">
      <el-form :inline="true" :model="filters" :size="size">
        <el-row>
          <el-form-item>
            <el-input v-model="filters.number" placeholder="规则编号" style="width: 200px;"></el-input>
          </el-form-item>

          <el-form-item>
            <el-select
              v-model="filters.shop_id"
              style="width: 200px;"
              filterable
              placeholder="所属店铺"
            >
              <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-date-picker
              v-model="filters.startKill"
              type="date"
              placeholder="秒杀时间始"
             
               style="width: 200px;"
            ></el-date-picker>
          </el-form-item>

          <el-form-item>
            <el-date-picker
              v-model="filters.endKill"
              type="date"
              placeholder="秒杀时间末"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd"
               style="width: 200px;"
            ></el-date-picker>
          </el-form-item>

          <el-form-item>
            <el-date-picker
              v-model="filters.startMin"
              type="date"
              placeholder="开始时间始"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd"
               style="width: 200px;"
            ></el-date-picker>
          </el-form-item>

          <el-form-item>
            <el-date-picker
              v-model="filters.startMax"
              type="date"
              placeholder="开始时间末"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd"
               style="width: 200px;"
            ></el-date-picker>
          </el-form-item>

          <el-form-item>
            <el-date-picker
              v-model="filters.endMin"
              type="date"
              placeholder="结束时间始"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd"
              style="width: 200px;"
            ></el-date-picker>
          </el-form-item>

          <el-form-item>
            <el-date-picker
              v-model="filters.endMax"
              type="date"
              placeholder="结束时间末"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd"
              style="width: 200px;"
            ></el-date-picker>
          </el-form-item>
   
   
          <el-row>
            
            
            <el-form-item>
              <kt-button
                label="新增秒杀规则"
                type="primary"
                @click="handleAdd"
              />
            </el-form-item>
            <el-form-item>
              <kt-button
                label="启用"
                type="primary"
                @click="startRule"
              />
            </el-form-item>
            <el-form-item>
              <kt-button
                label="停用"
                type="primary"
                @click="endRule"
              />
            </el-form-item>
            <el-form-item>
              <kt-button
                label="关联活动商品"
                type="primary"
                @click="contactPro"
              />
            </el-form-item>
            
              
             <div style="float:right">
              <el-form-item >
              <kt-button
                icon="fa fa-search"
                :label="$t('action.search')"
                type="primary"
                @click="findPage(null)"
              />
            </el-form-item>
            <el-form-item>
              <kt-button
                icon="fa fa-refresh" :label="$t('action.reset')"
                type="primary"
                @click="resetForm('filters')"
              />
            </el-form-item>
             </div>
          </el-row>

        

      
        </el-row>
      </el-form>
    </div>
    <div class="toolbar" style="float:right;padding-top:10px;padding-right:15px;">
      <el-form :inline="true" :size="size">
        <el-form-item>
          <el-button-group>
            <el-tooltip content="刷新" placement="top">
              <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
            </el-tooltip>
            <el-tooltip content="列显示" placement="top">
              <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
            </el-tooltip>
            <el-tooltip content="导出" placement="top">
              <el-button icon="fa fa-file-excel-o" @click="downloadExcel" id="downloadExcel"></el-button>
            </el-tooltip>
          </el-button-group>
        </el-form-item>
      </el-form>
      <!--表格显示列界面-->
      <table-column-filter-dialog
        ref="tableColumnFilterDialog"
        :columns="columns"
        @handleFilterColumns="handleFilterColumns"
      ></table-column-filter-dialog>
    </div>
    <!--表格内容栏-->
    <cy-table
      :height="350"
      ref="CyTable"
      :data="pageResult"
      :columns="filterColumns"
     @selectionChange="selectionChange"
      @findPage="findPage"
      @handleEdit="handleEdit"
      @handleDelete="handleDelete"
    ></cy-table>
    <!--新增编辑界面-->
    <el-dialog
      :title="operation?'新增':'编辑'"
      width="40%"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
    >
      <el-form
        :model="dataForm"
        label-width="80px"
        :rules="dataFormRules"
        ref="dataForm"
        :size="size"
        label-position="right"
      >
        
        <el-form-item label="所属商铺" prop="shop_id">
          <el-select
            v-model="dataForm.shop_id"
             value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
            placeholder="所属店铺"
            style="width: 100%;"
          >
            <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>

         <el-form-item label="秒杀时间" prop="kill_time">
          <el-date-picker
            v-model="dataForm.kill_time"
            style="width:100%"
            type="date"
             value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
            placeholder="选择日期"
          ></el-date-picker>
        </el-form-item>

         <el-form-item label="循环开始时间" prop="start"   >
          <el-date-picker
            v-model="dataForm.start"
            style="width:100%"
            type="date"
             format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
            placeholder="选择日期"
            v-on:input = "watchStart"
          ></el-date-picker>
        </el-form-item>

         <el-form-item label="循环结束时间" prop="end">
          <el-date-picker
            v-model="dataForm.end"
            style="width:100%"
            type="date"
             format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
            placeholder="选择日期"
             v-on:input = "watchEnd"
          ></el-date-picker>
        </el-form-item>

        <el-form-item label="秒杀价格" prop="money">
          <el-input type="number" v-model="dataForm.money" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :size="size" @click.native="dialogVisible = false">{{$t('action.cancel')}}</el-button>
        <el-button
          :size="size"
          type="primary"
          @click.native="submitForm"
          :loading="editLoading"
        >{{$t('action.submit')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import PopupTreeInput from "@/components/PopupTreeInput";
import CyTable from "@/views/Core/CyTable";
import KtButton from "@/views/Core/KtButton";
import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
import { format } from "@/utils/datetime";
import { exportExcel } from "@/utils/excel";
export default {
  components: {
    PopupTreeInput,
    CyTable,
    KtButton,
    TableColumnFilterDialog
  },
  data() {
    return {
      size: "small",
      filters: {
        number: "",
        shop_id: "",
        startKill:"",
        endKill:"",
        startMin:"",
        startMax:"",
        endMin:"",
        endMax:""
      },
      columns: [],
      filterColumns: [],
      pageRequest: { pageNum: 1, pageSize: 10 },
      pageResult: {},
      options: {},
      operation: false, // true:新增, false:编辑
      dialogVisible: false, // 新增编辑界面是否显示
      editLoading: false,
      dataFormRules: {
        shop_id: [{ required: true, message: "请选择商铺", trigger: "blur" }],
        kill_time: [{ required: true, message: "请输入秒杀时间", trigger: "blur" }],
        start: [{ required: true, message: "请输入开始时间", trigger: "blur" }],
        end: [{ required: true, message: "请输入结束时间", trigger: "blur" }],
        money: [{ required: true, message: "请输入秒杀价格", trigger: "blur" }]
      },

      // 新增编辑界面数据
      dataForm: {
        shop_id: "",
        kill_time: "",
        start: "",
        end: "",
        money:""
      },
      selections:[],  
      
    };
  },
  methods: {
      //动态监听日期
     watchStart(){
       var this_ = this;
      let starttime =this_.dataForm.start;
      let endtime = this_.dataForm.end;
        if(new Date(starttime).getTime()>=new Date(endtime).getTime()){
          this.$message({ message: "开始日期不能晚于结束日期 ", type: "error"});
          this_.dataForm.start = ""
          return false;
        }
     },

     watchEnd(){
     var this_ = this;
      let starttime =this_.dataForm.start;
      let endtime = this_.dataForm.end;
        if(new Date(endtime).getTime()<=new Date(starttime).getTime()){
          this.$message({ message: "结束日期不能早于开始日期 ", type: "error"});
          this_.dataForm.end = ""
          return false;
        }
     },

    //关联商品
    contactPro: function() {
      if (this.selections.length != 1) {
        this.$message({ message: "请选择一条数据", type: "error" });
        
        return;
      } else {
        this.$router.push({ path:'/product/productEntry',query:{killid: this.selections[0].id}})   
      }
    },
    selectionChange:function(data){
      this.selections = data.selections;
    },
    // 获取分页数据
    findPage: function(data) {
      this.filters.t = "marketSeckill";
      this.$refs.CyTable.findPage(this.filters);
    },

    // 批量删除
    handleDelete: function(data) {
      var ids = "";
      for (var i = 0; i < data.params.length; i++) {
        ids = ids + data.params[i].id + ",";
      }
      data.params.t = "marketSeckill";
      data.params.ids = ids;
      this.utils.request.deleteUserInfo(data.params, data.callback);
    },
    // 显示新增界面
    handleAdd: function() {
      this.dialogVisible = true;
      this.operation = true;
      this.dataForm = {};
     
      console.log(dataForm);
    },
    //列表下载
    downloadExcel() {
      this.$confirm("确定下载列表文件?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let filename = "秒杀配置";
          this.$refs.CyTable.exportData(
            this.filters,
            this.filterColumns,
            filename
          );
        })
        .catch(() => {});
    },
    // 显示编辑界面
    handleEdit: function(params) {
      console.log(params);
      this.dialogVisible = true;
      this.operation = false;
      this.dataForm = Object.assign({}, params.row);
    },

   
    // 编辑
    submitForm: function() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.$confirm("确认提交吗？", "提示", {}).then(() => {
            this.editLoading = true;
            let params = Object.assign({}, this.dataForm);
            params.t = "marketSeckill";
            this.utils.request.saveSeckill(params, this.saveSeckillBack);
          });
        }
      });
    },

    // 新增修改回调
    saveSeckillBack: function(data) {
      if (data.code == "0000") {
        this.$message({ message: "操作成功", type: "success" });
        this.dialogVisible = false;
        this.$refs["dataForm"].resetFields();
      } else {
        this.$message({ message: "操作失败, " + data.msg, type: "error" });
      }
      this.findPage({ t: "marketSeckill" });
      this.dialogVisible = false;
      this.operation = false;
      this.editLoading = false;
    },
    // 时间格式化
    dateFormat: function(row, column, cellValue, index) {
      return format(row[column.property]);
    },
    // 处理表格列过滤显示
    displayFilterColumnsDialog: function() {
      this.$refs.tableColumnFilterDialog.setDialogVisible(true);
    },
    // 处理表格列过滤显示
    handleFilterColumns: function(data) {
      this.filterColumns = data.filterColumns;
      this.$refs.tableColumnFilterDialog.setDialogVisible(false);
    },
    // 处理表格列过滤显示
    initColumns: function() {
      this.columns = [
        { prop: "id", label: "ID", minWidth: 50 },
        { prop: "number", label: "规则编号", minWidth: 120 },
        { prop: "shopname", label: "所属商铺", minWidth: 120 },
        { prop: "kill_time", label: "秒杀时间", minWidth: 120 },
        { prop: "start", label: "开始时间", minWidth: 120 },
        { prop: "end", label: "结束时间", minWidth: 120 },
        { prop: "money", label: "价格", minWidth: 120 },
        { prop: "statusval", label: "状态", minWidth: 120 },
        { prop: "create_time", label: "创建时间", minWidth: 120 },
        { prop: "creatorname", label: "创建人", minWidth: 120 },
        { prop: "update_time", label: "修改时间", minWidth: 120 },
        { prop: "updatorname", label: "修改人", minWidth: 120 },
      ];
      var temp = [];
      $.each(this.columns, function(key, val) {
        temp.push(val);
      });
      this.filterColumns = temp;
    },
    resetForm(formName) {
      this.filters = {
        name: "",
        code: "",
        types: "",
        sortno: ""
      };
      this.findPage({ t: "marketSeckill" });
    },
    // 所属商品分类
    queryProType() {
      var that = this;
      this.utils.request.queryUserList({ t: "shop" }, function(data) {
        that.options = data.data;
      });
    },
    //启用停用按钮
    //启用
    startRule: function() {
      if (this.selections.length != 1) {
        this.$message({ message: "请选择一条数据", type: "error" });
        return;
      } else {
        var data = {};
        data.id = data.id = this.selections[0].id;
        data.t="marketSeckill";
        data.status = 1;

        var this_ = this;
        this.utils.request.editUserInfo(data, function(res) {
          if (res.code == "0000") {
            this_.$message({ message: "启用成功", type: "success" });
            this_.checkpage = false;
            this_.findPage();
          } else {
            this_.$message({ message: res.msg, type: "error" });
          }
        });
      }

      this.checkpage = true;
    },

   //停用按钮
    endRule: function() {
      if (this.selections.length != 1) {
        this.$message({ message: "请选择一条数据", type: "error" });
        return;
      } else {
        var data = {};
        data.id = this.selections[0].id;
        data.t="marketSeckill";
        data.status = 0;

        var this_ = this;
        this.utils.request.editUserInfo(data, function(res) {
          if (res.code == "0000") {
            this_.$message({ message: "停用成功", type: "success" });
            this_.checkpage = false;
            this_.findPage();
          } else {
            this_.$message({ message: res.msg, type: "error" });
          }
        });
      }

      this.checkpage = true;
    },
  },

  mounted() {
    this.initColumns();
    this.queryProType();
  }
};
</script>

<style scoped>
</style>